package com.powernode.controller;


import com.powernode.common.Result;
import com.powernode.domain.SysMenu;
import com.powernode.enums.ResultEnums;
import com.powernode.service.SysMenuService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/sysMenu")
public class SysMenuController extends BaseController{
    @Resource
    private SysMenuService sysMenuService;

    /**
     * 查询所有菜单权限
     * GET http://127.0.0.1:8080/sysMenu/listMenu
     * 接口ID：60111884
     * 接口地址：https://www.apifox.cn/web/project/2244992/apis/api-60111884
     */
    @GetMapping("/listMenu")
    public Result<?> listDept(SysMenu sysMenu){
        List<SysMenu> sysMenuList=sysMenuService.queryAllDeptList(sysMenu,getCurrentUser());
        return new Result<>(sysMenuList);
    }

    /**
     * 添加菜单权限
     * POST http://127.0.0.1:8080/sysMenu/save
     * 接口ID：60111886
     * 接口地址：https://www.apifox.cn/web/project/2244992/apis/api-60111886
     */
    @PostMapping("save")
    public Result<?> save(@RequestBody SysMenu sysMenu){
        //创建时间
        sysMenu.setCreateTime(new Date());
        sysMenu.setCreateBy(getUsername());
        sysMenuService.insertSelective(sysMenu);
        return new Result("添加成功");
    }

    /**
     * 修改菜单权限
     * PUT http://127.0.0.1:8080/sysMenu/update
     * 接口ID：60111885
     * 接口地址：https://www.apifox.cn/web/project/2244992/apis/api-60111885
     */
    @PutMapping("update")
    public Result update(@RequestBody SysMenu menu){
        //修改时间
        menu.setUpdateTime(new Date());
        menu.setUpdateBy(getUsername());
        sysMenuService.updateByPrimaryKeySelective(menu);
        return new Result("修改成功");
    }

    /**
     * 根据菜单ID查询菜单
     * GET http://127.0.0.1:8080/sysMenu/get/{menuId}
     * 接口ID：60111888
     * 接口地址：https://www.apifox.cn/web/project/2244992/apis/api-60111888
     */
    @GetMapping("get/{menuId}")
    public Result get(@PathVariable Long menuId){
        SysMenu sysMenu = sysMenuService.selectByPrimaryKey(menuId);
        return new Result(sysMenu);
    }

    /**
     * 删除菜单权限
     * DELETE http://127.0.0.1:8080/sysMenu/delete/{menuId}
     * 接口ID：60111887
     * 接口地址：https://www.apifox.cn/web/project/2244992/apis/api-60111887
     */
    @DeleteMapping("delete/{menuId}")
    public Result delete(@PathVariable Long menuId){
        //根据deptId查询这个deptId下面子节点个数[使用deptId传后数据库查询有没有parentId=这个deptId条数，如果条数大于0 就说明这个deptId下面有子节点]
        Integer count=sysMenuService.queryChildrenCountByDeptId(menuId);
        if(count>0){
            return new Result(ResultEnums.ERROR,"当前部门下面有子部门，请先删除子部门");
        }
        sysMenuService.deleteByPrimaryKey(menuId);
        return new Result("删除成功");
    }


    /**
     * 只查询菜单和目录
     * GET http://127.0.0.1:8080/sysMenu/listMenuMC
     * 接口ID：43445087
     * 接口地址：https://www.apifox.cn/web/project/1739100/apis/api-43445087
     */
    @GetMapping("listMenuMC")
    public Result listMenuMC(){
        List<SysMenu> sysMenuList=sysMenuService.queryAllMenuListMC(getCurrentUser());
        return new Result(sysMenuList);
    }
}
